package kotan.util;

import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord;

/**
 * 
 * @author shuji.w6e
 * @since 1.0
 */
public class LogFormatter extends Formatter {

    @Override
    public String format(LogRecord record) {
        StringBuilder str = new StringBuilder();
        Level lv = record.getLevel();
        if (lv == Level.INFO) {
            str.append("INFO:  ");
        } else if (lv == Level.WARNING) {
            str.append("WARN:  ");
        } else if (lv == Level.SEVERE) {
            str.append("ERROR: ");
        } else {
            str.append("DEBUG: ");
        }
        str.append("[").append(record.getLoggerName()).append("]");
        str.append(" ").append(record.getMessage());
        str.append("\n");
        if (record.getThrown() != null) {
            Throwable t = record.getThrown();
            str.append(t.getClass().getName()).append(": ").append(t.getMessage()).append("\n");
            for (StackTraceElement trace : t.getStackTrace()) {
                str.append(trace.toString()).append("\n");
            }
        }
        return str.toString();
    }

}
